<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      class="form-search"
      label-width="120px">
      <el-form-item :label="$t('payinFD.exchangeName')"
        prop="exchangeName">
        <el-select v-model="queryParams.exchangeId"
          filterable
          remote
          reserve-keyword
          :placeholder="$t('common.placeholderSelect')"
          :remote-method="remoteMethod"
          :loading="exhloading"
          style="width: 240px">
          <el-option v-for="item in exchangeList"
            :key="item.exchangeId"
            :label="item.exchangeName"
            :value="item.exchangeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('payinFD.dateRange')">
        <el-date-picker v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <div class="section-box">
      <el-row :gutter="10"
        class="mb8">
        <el-link type="warning">
          <span>{{$t('payinFD.amountTotal')}}：{{totalData.amount || 0}} {{$t('common.unit')}}</span>
          <span class="mr20 ml20">{{$t('payinFD.payTotal')}}：{{totalData.transAmount || 0}}</span>
          <span>{{$t('payinFD.progress')}}：{{totalData.progress || 0}}%</span>
        </el-link>
      </el-row>

      <el-table v-loading="loading"
        :data="dataList">
        <!-- <el-table-column type="selection"
        min-width="55"
        align="center" /> -->
        <el-table-column :label="$t('payinFD.exchangeName')"
          align="center"
          prop="exchangeName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('payinFD.predictionDate')"
          align="center"
          prop="predictionDate"
          min-width="160" />
        <el-table-column :label="$t('payinFD.amount')"
          align="center"
          prop="amount"
          min-width="120" />
        <el-table-column :label="$t('payinFD.payin')"
          align="center"
          prop="transAmount"
          min-width="120" />
        <el-table-column :label="$t('payinFD.progress')"
          align="center"
          prop="progress"
          min-width="100">
          <template slot-scope="scope">
            <span>{{scope.row.progress || 0}}%</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('payinFD.deadline')"
          align="center"
          prop="deadline"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.controls')"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="180">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)">{{$t('common.view')}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

  </div>
</template>

<script>
import {
  getForecastDataList,
  forecastTatal
} from "@/api/transaction/payinForecastData.js";
import {
  getExhUserList,
} from "@/api/system/exchangeUser.js";
import { dateFormat } from '@/utils/util.js'

export default {
  name: "PayinForecastData",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exchangeId: null,
        startDate: null,
        endDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      totalData: {},
      exchangeList: [],
      exhloading: false,

    };
  },
  watch: {
    dateRange (val) {
      if (val) {
        this.queryParams.startDate = val[0]
        this.queryParams.endDate = val[1]
      } else {
        this.queryParams.startDate = null
        this.queryParams.endDate = null
      }
    },
  },
  created () {
    this.dateRange = [dateFormat(new Date(), "yyyy-MM-dd"), dateFormat(new Date(), "yyyy-MM-dd")]
    this.queryParams.startDate = dateFormat(new Date(), "yyyy-MM-dd")
    this.queryParams.endDate = dateFormat(new Date(), "yyyy-MM-dd")
    this.getList();
    this.getDataTotal()
  },
  methods: {
    // 搜索汇兑用户
    remoteMethod (query) {
      if (query !== '') {
        this.exhloading = true;
        getExhUserList({
          pageNum: 1,
          pageSize: 10,
          payType: 'payin',
          status: 1,
          exchangeName: query
        }).then(
          (response) => {
            this.exchangeList = response.rows;
            this.exhloading = false;
          }
        );
      } else {
        this.exchangeList = [];
      }
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      getForecastDataList({
        ...this.queryParams,
        startDate: `${this.queryParams.startDate} 00:00:00`,
        endDate: `${this.queryParams.endDate} 23:59:59`,
      }).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 查询预告汇总
    getDataTotal () {
      forecastTatal({
        ...this.queryParams,
        startDate: `${this.queryParams.startDate} 00:00:00`,
        endDate: `${this.queryParams.endDate} 23:59:59`,
      }).then(
        (response) => {
          this.totalData = response.data
        }
      );
    },
    // 查看详情
    handleView (row) {
      this.$router.push({
        path: "/transaction/payin-forecastData/payinForecastDetail",
        query: {
          exchangeId: row.exchangeId,
          predictionDate: row.predictionDate,
        }
      });
    },

    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getDataTotal()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.exchangeList = []
      this.queryParams.exchangeId = ''
      this.dateRange = [dateFormat(new Date(), "yyyy-MM-dd"), dateFormat(new Date(), "yyyy-MM-dd")]
      this.queryParams.startDate = dateFormat(new Date(), "yyyy-MM-dd")
      this.queryParams.endDate = dateFormat(new Date(), "yyyy-MM-dd")
      this.handleQuery();
    },

  },
};
</script>
